<?php

require_once dirname(__FILE__).'/../lib/BaseygmPerCredentialActions.class.php';

/**
 * ygmPerCredential actions.
 * 
 * @package    ygmPermissionSystemPlugin
 * @subpackage ygmPerCredential
 * @author     Yuri Gauer Marques <yurigmarques@gmail.com>
 * @version    SVN: $Id: actions.class.php 12534 2008-11-01 13:38:27Z Kris.Wallsmith $
 */
class ygmPerCredentialActions extends BaseygmPerCredentialActions
{protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            try {

                if ($request->getParameter('id')) {
                    $ygm_per_credentialAntes = Doctrine_Core::getTable('YgmPerCredential')
                                    ->find($request->getParameter('id'))->ygmPerModuleCredential;
                    $request->setParameter('module_anterior', $ygm_per_credentialAntes);
                }
                $ygm_per_credential = $form->save();
                $request->setParameter('result_save', $ygm_per_credential);

                //Salva Imagem espelho OFF
                $img = new sfImage(sfConfig::get('sf_upload_dir') . '/credential/' . $ygm_per_credential->getImagen(), 'image/png');
                $nomeImagem = explode('.', $ygm_per_credential->getImagen());
                $imgpb = new sfImageGreyscaleGD();
                $img = $imgpb->execute($img);
                $img->saveAs(sfConfig::get('sf_upload_dir') . '/credential/' . $nomeImagem[0] . '_off.png');
                //----

                $moduleCredential = new YgmPerModuleCredential();
                $moduleCredential->setPermissions($request);
            } catch (Doctrine_Validator_Exception $e) {

                $errorStack = $form->getObject()->getErrorStack();

                $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
                foreach ($errorStack as $field => $errors) {
                    $message .= "$field (" . implode(", ", $errors) . "), ";
                }
                $message = trim($message, ', ');

                $this->getUser()->setFlash('error', $message);
                return sfView::SUCCESS;
            }

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $ygm_per_credential)));

            if ($request->hasParameter('_save_and_add')) {
                $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                $this->redirect('@ygm_per_credential_new');
            } else {
                $this->getUser()->setFlash('notice', $notice);

                $this->redirect(array('sf_route' => 'ygm_per_credential_edit', 'sf_subject' => $ygm_per_credential));
            }
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();
        $infosPermissions['permissionsId'] = array();
        $infosPermissions['modulesId'] = array();
        foreach ($this->getRoute()->getObject()->ygmPerModuleCredential as $info) {
            $infosPermissions['permissionsId'][] = $info->getPermissionId();
        }
        foreach ($this->getRoute()->getObject()->Modules as $modulo) {
            $infosPermissions['modulesId'][] = $modulo->getId();
        }
        $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

        if ($this->getRoute()->getObject()->delete()) {

            $imagenName = explode('.', $this->getRoute()->getObject()->getImagen());
            unlink(sfConfig::get('sf_upload_dir') . '/credential/' . $imagenName[0] . '_off.png');
            unlink(sfConfig::get('sf_upload_dir') . '/credential/' . $this->getRoute()->getObject()->getImagen());

            $moduleCredential = new YgmPerModuleCredential();
            //---------
            $moduleCredential->deletePermissionsCredential($infosPermissions);
            $this->getUser()->setFlash('notice', 'The item was deleted successfully.');
        }

        $this->redirect('@ygm_per_credential');
    }

    protected function executeBatchDelete(sfWebRequest $request) {
        $ids = $request->getParameter('ids');

        $records = Doctrine_Query::create()
                ->from('ygmPerCredential')
                ->whereIn('id', $ids)
                ->execute();

        foreach ($records as $record) {
            $infosPermissions['permissionsId'] = array();
            $infosPermissions['modulesId'] = array();
            foreach ($record->ygmPerModuleCredential as $info) {
                $infosPermissions['permissionsId'][] = $info->getPermissionId();
            }
            foreach ($record->Modules as $modulo) {
                $infosPermissions['modulesId'][] = $modulo->getId();
            }
            $imagenName = explode('.', $record->getImagen());
            unlink(sfConfig::get('sf_upload_dir') . '/credential/' . $imagenName[0] . '_off.png');
            unlink(sfConfig::get('sf_upload_dir') . '/credential/' . $record->getImagen());

            $moduleCredential = new YgmPerModuleCredential();
            //---------
            $moduleCredential->deletePermissionsCredential($infosPermissions);

            $record->delete();
        }

        $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
        $this->redirect('@ygm_per_credential');
    }
}
